/************************************************************************************
* StarWidget.cpp                                                                    *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "StarWidget.h"
#include "common/Utils.h"

#include <QPainter>
#include <QMouseEvent>
#include <qdebug.h>
#include <math.h>


//QPixmap* StarWidget::m_star    = NULL;
//QPixmap* StarWidget::m_sadStar = NULL;


StarWidget::StarWidget(MediaRating rating, QWidget *parent) :
    QWidget(parent) {
    m_rating = rating;
    m_floatingRating = rating;
    m_star = new QPixmap(":/rating");
    m_sadStar = new QPixmap(":/rating_disabled");
    setMouseTracking(true);
}


void StarWidget::paintEvent(QPaintEvent *event) {
    QPainter pxPaint(this);
    int rating = Utils::getIntegerFromRating(m_floatingRating);
    for(int i = 0; i < 5; ++i) {
        if(i < rating) {
            pxPaint.drawPixmap(i * m_star->width(), 0, *m_star);
        }
        else {
//            pxPaint.fillRect(i * m_sadStar->width(), 0, 16, 16, QBrush(Qt::white));
            pxPaint.drawPixmap(i * m_sadStar->width(), 0, *m_sadStar);
        }
    }
}



void StarWidget::mouseReleaseEvent(QMouseEvent *event) {
    if(m_floatingRating != m_rating) {
        m_rating = m_floatingRating;
        emit ratingSet(m_rating);
    }
}



StarWidget::~StarWidget() {
    delete m_star;
    delete m_sadStar;
}



QSize StarWidget::sizeHint() const {
    return QSize((MAX_STARS * m_star->width()), m_star->height());
}



void StarWidget::mouseMoveEvent(QMouseEvent *event) {
    float xpos = event->x();
    float ypos = event->y();
    float rating = static_cast<int>(ceil(((xpos - 8) / m_star->width())));


    if( xpos >= sizeHint().width() - 4 ||
        xpos - 4 <= 0                  ||
        ypos >= sizeHint().height() - 2||
        ypos - 2  <= 0) {
        m_floatingRating = m_rating;
        update();
        return;
    }
    else {
        m_floatingRating = Utils::getMediaRating(rating);
        update();
    }
}



void StarWidget::paint(MediaRating rat, QPainter *painter, int x, int y) {
    //    initStars();
    QPixmap star(":/rating");
    QPixmap sadStar(":/rating_disabled");

    int rating = Utils::getIntegerFromRating(rat);
    for(int i = 0; i < 5; ++i) {
        if(i < rating) {
            painter->drawPixmap(x + i * star.width(), y , star);
        }
        else {
            painter->drawPixmap(x + i * sadStar.width(), y , sadStar);
        }
    }
}



void StarWidget::setRating(MediaRating rating) {
    m_rating = rating;
    m_floatingRating = rating;
    update();
}


MediaRating StarWidget::rating() {
    return m_rating;
}


MediaRating StarWidget::floatingRating() {
    return m_floatingRating;
}
